<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 20px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加管理员</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item  label="用户名" prop="username" >
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <el-input v-model="ruleForm.avatar"></el-input>
      </el-form-item>

      <el-form-item label="角色" prop="roleIds">
          <el-select v-model="ruleForm.roleIds" multiple placeholder="请选择">
            <el-option
                v-for="item in roleListOption"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
      </el-form-item>

        <el-form-item label="是否启用"  prop="enable">
        <el-switch
            v-model="ruleForm.enable"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949">
        </el-switch>
        </el-form-item>

<!--      <div style="position: relative;left: 50px;top: 20px">-->
<!--        角色-->
<!--        <el-select v-model="value" placeholder="请选择">-->
<!--          <el-option-->
<!--              v-for="item in options"-->
<!--              :key="item.value"-->
<!--              :label="item.label"-->
<!--              :value="item.value">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </div>-->


      <el-form-item style="position: relative;top: 50px">
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      roleListOption: [],
      ruleForm: {
        username:"龙王",
        password:"123456",
        nickname:"龙龙",
        description:"最厉害",
        phone:"123456",
        email:"123@qq.com",
        avatar:"adw5d54ad4",
        enable:1,
        roleIds: [],
      },
      value: true,
      rules: {
        username: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在1 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入关键词', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 8到 15 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { min: 1, max: 35, message: '长度在 1 到 35 个字符', trigger: 'blur' }
        ],
        avatar: [
          { required: true, message: '请输入头像', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ],
        roleIds: [
          { required: true, message: '至少选择一个角色', trigger: 'blur' },
        ]
      }
    };
  },
  methods: {
    loadRoleList(){
      let url = 'http://localhost:9081/roles';
      console.log('url='+url);
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        let responseBody = response.data;
        this.roleListOption=responseBody.data;
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url ='http://localhost:9081/admins/add-new';
          console.log('url='+url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData='+formData);
          this.axios
              .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
              .post(url ,formData).then(response => {
            let responseBody = response.data;
            console.log('responseBody='+responseBody);
            if (responseBody.state==10000){
              this.resetForm(formName);
            }else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadRoleList();
  }
}
</script>
